/*---------------------------Private Function-------------------------------*/
const TIMEOUT_LOADING = 1;
function _isUndefined(variable) {
    return(variable === null || typeof(variable) === 'undefined');
}
function _redirect(url){
    if(!_isUndefined(url)){
        window.location.href = url;
    }
}
function _getFormData(form) {
    var data = {};
    form.find('input[name],select[name],textarea[name]').each(function () {
        var name = jQuery(this).attr('name');
        data[name] = jQuery(this).val();
    });
    return data;
}
function _bindFormData(from,to){
    to.find('input[name],select[name],textarea[name]').each(function () {
        var obj = jQuery(this);
        var name = obj.attr('name');
        var value = from.find('[name='+name+']');
        if(value.length>0){
            obj.val(value.val());
        }
    });
}
function _alert(content) {
    alert(content);
}
function _showError(content, obj) {
    if (!_isUndefined(content)) {
        if (_isUndefined(obj)) {
            _alert(content);
        }
        else {
            obj.text(content);
            setTimeout(function(){obj.parent().show();},TIMEOUT_LOADING);
        }
    }
}
function _ajaxSubmit(obj,successFunc,beforeFunc){
    jQuery.ajax({
        url: obj.attr('action'),
        type: 'POST',
        data: _getFormData(obj),
        beforeSend: function () {
            jQuery('.message-box >div').hide();
            if(!_isUndefined(beforeFunc)){
                beforeFunc();
            }
        },
        success: function (data) {
            try {
                var json = JSON.parse(data);
                if (json.status) {
                    if (json.code == 'redirect') {
                        setTimeout(function () {
                            _redirect(json.content);
                        }, TIMEOUT_LOADING);
                    }
                    if(json.code == 'message'){
                        _showError(json.content,jQuery('.message-box .mes-success span'));
                    }
                    if(!_isUndefined(json.html)){
                        for(var i=0;i<json.html.length;i++){
                           var item = json.html[i];
                           jQuery(item[0]).html(jQuery(item[1]).html());
                        }
                    }
                    if(!_isUndefined(successFunc)){
                        successFunc(json);
                    }
                }
                else {
                    if (json.code == 'error') {
                        _showError(json.content,jQuery('.message-box .mes-error span'));
                    }
                    if (json.code == 'csrf') {
                        _showError(json.content);
                        window.location.reload();
                    }
                }
            }
            catch (e) {
                _showError('Invalid Json');
            }
        }
    });
}
/*--------------------------ON Function------------------------------*/
/*-------Body-------*/
var _BodyClick = true;
jQuery(document).on('click', function () {
    if (_BodyClick) {
    }
    else {
        _BodyClick = true;
    }
});
